
Imports System.Drawing
Imports System.Drawing.Imaging

Namespace graphUtils
    Public Module graphUtils

        Public Sub saveas(ByVal img As Image, ByVal fileName As String _
                          , ByVal format As Imaging.ImageFormat)
            img.Save(fileName, format)
        End Sub

        Public Function name2format(ByVal name As String _
                                     , Optional ByVal defaultFormat As ImageFormat = Nothing) As ImageFormat
            If defaultFormat Is Nothing Then
                defaultFormat = ImageFormat.Png
            End If

            Select Case name.ToLower()
                Case "bmp", ImageFormat.Bmp.ToString().ToLower()
                    Return ImageFormat.Bmp
#If Not (PocketPC OrElse Smartphone) Then
                Case "emf", ImageFormat.Emf.ToString().ToLower()
                    Return ImageFormat.Emf
                Case "exif", ImageFormat.Exif.ToString().ToLower()
                    Return ImageFormat.Exif
#End If
                Case "gif", ImageFormat.Gif.ToString().ToLower()
                    Return ImageFormat.Gif
#If Not (PocketPC OrElse Smartphone) Then
                Case "ico", "icon", ImageFormat.Icon.ToString().ToLower()
                    Return ImageFormat.Icon
                Case "jpg", "jpeg", ImageFormat.Jpeg.ToString().ToLower()
                    Return Imaging.ImageFormat.Jpeg
                Case "memorybmp", ImageFormat.MemoryBmp.ToString().ToLower()
                    Return ImageFormat.MemoryBmp
#End If
                Case "png", ImageFormat.Png.ToString().ToLower()
                    Return ImageFormat.Png
#If Not (PocketPC OrElse Smartphone) Then
                Case "tiff", ImageFormat.Tiff.ToString().ToLower()
                    Return ImageFormat.Tiff
                Case "wmf", ImageFormat.Wmf.ToString().ToLower()
                    Return ImageFormat.Wmf
#End If
                Case Else
                    Return defaultFormat
            End Select
        End Function

        Public Function resizeImage(ByVal image As Image, ByVal destSize As Size) As Image
            If image Is Nothing Then
                Return Nothing
            End If

            Dim rtn As Image = Nothing
#If Not (PocketPC OrElse Smartphone) Then
            rtn = New Bitmap(destSize.Width, destSize.Height, image.PixelFormat)
#Else
            rtn = New Bitmap(destSize.Width, destSize.Height)
#End If
            Dim grap As Graphics = Nothing
            grap = Graphics.FromImage(rtn)
            grap.DrawImage(image, New Rectangle(0, 0, destSize.Width, destSize.Height) _
                           , New Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel)
            grap.Dispose()
            Return rtn
        End Function

#If Not (PocketPC OrElse Smartphone) Then
        Public Function thumbnailImage(ByVal image As Image, ByVal destSize As Size) As Image
            If image Is Nothing Then
                Return Nothing
            End If
            Return image.GetThumbnailImage(destSize.Width, destSize.Height, Nothing, Nothing)
        End Function
#End If

    End Module
End Namespace
